package com.haixiaoke.business;

import com.haixiaoke.common.annotation.Log;
import com.haixiaoke.common.core.domain.AjaxResult;
import com.haixiaoke.common.core.page.TableDataInfo;
import com.haixiaoke.common.enums.BusinessType;
import com.haixiaoke.common.utils.poi.ExcelUtil;
import com.haixiaoke.domain.Brand;
import com.haixiaoke.service.IBrandService;
import com.haixiaoke.system.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 品牌Controller
 *
 * @author xiaolong
 * @date 2023-05-24
 */
@RestController
@RequestMapping("/base/brand")
public class BrandController extends BaseController {
    @Autowired
    private IBrandService brandService;

    /**
     * 查询品牌列表
     */
    @PreAuthorize("@ss.hasPermi('business:brand:list')")
    @GetMapping("/list")
    public TableDataInfo list(Brand brand) {
        startPage();
        List<Brand> list = brandService.selectBrandList(brand);
        return getDataTable(list);
    }

    /**
     * 导出品牌列表
     */
    @PreAuthorize("@ss.hasPermi('business:brand:export')")
    @Log(title = "品牌", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Brand brand) {
        List<Brand> list = brandService.selectBrandList(brand);
        ExcelUtil<Brand> util = new ExcelUtil<Brand>(Brand.class);
        util.exportExcel(response, list, "品牌数据");
    }

    /**
     * 获取品牌详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:brand:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(brandService.selectBrandById(id));
    }

    /**
     * 新增品牌
     */
    @PreAuthorize("@ss.hasPermi('business:brand:add')")
    @Log(title = "品牌", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Brand brand) {
        return toAjax(brandService.insertBrand(brand));
    }

    /**
     * 修改品牌
     */
    @PreAuthorize("@ss.hasPermi('business:brand:edit')")
    @Log(title = "品牌", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Brand brand) {
        return toAjax(brandService.updateBrand(brand));
    }

    /**
     * 删除品牌
     */
    @PreAuthorize("@ss.hasPermi('business:brand:remove')")
    @Log(title = "品牌", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(brandService.deleteBrandByIds(ids));
    }

    /**
     * 删除型号和品牌
     */
    @DeleteMapping("/delete")
    public AjaxResult delete(@RequestParam(value = "modelId") Long modelId,@RequestParam(value = "brandId") Long brandId) {
        return toAjax(brandService.deleteModelAndBrand(modelId,brandId));
    }

    /**
     * 下拉选择需要的option
     */
    @GetMapping("/option")
    public AjaxResult option(Brand brand) {
        return AjaxResult.success(brandService.selectBrandList(brand));
    }

    /**
     * 级联选择器要用的option
     */
    @GetMapping("/cascader")
    public AjaxResult cascader() {
        return AjaxResult.success(brandService.cascader());
    }

}
